﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;
using System.IO;
using System.Net.Sockets;
using System.Globalization;

namespace Warehouse
{
    public partial class Form1 : Form
    {
        string _connectionString;
        DBConnect _db;
        String _warehouse = "";
        String _ip = "";
        private System.Windows.Forms.Timer _refreshTimer = new System.Windows.Forms.Timer();

        /*private void refreshTimer()
        {
            _refreshTimer.Interval = 10000;
            _refreshTimer.Tick += new EventHandler(timer1_Tick);
            _refreshTimer.Start();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                // Not on UI thread, reenter there... 
                this.BeginInvoke(new EventHandler(timer1_Tick), sender, e);
            }
            else
            {
                lock (_refreshTimer)
                {
                    // only work when this is no reentry while we are already working 
                    if (this._refreshTimer.Enabled)
                    {
                        this._refreshTimer.Stop();
                        if (_db.getConnection().State != ConnectionState.Open)
                        {
                            MySqlCommand _command = _db.OpenConnection().CreateCommand();
                            showPO(_command);
                            showLokasiBarangBaru(_command);
                            showLokasi(_command);
                            showNamaBarang(_command);
                            showDaftarWarna(_command);
                            showBarcode(_command);
                            showCabang(_command);
                            MessageBox.Show("Refreshed");
                        } 
                        _db.CloseConnection();
                        this._refreshTimer.Start(); // optionally restart for periodic work 
                    }
                }
            }
        }*/
        
        public Form1()
        {
            Form3 form3 = new Form3(this);
            form3.ShowDialog();
            if (_warehouse != "")
            {
                InitializeComponent();
                _db = new DBConnect(_ip, "warehouse_" + _warehouse);
                _connectionString = _db.getDBString();
                //updateVersion();
                getDateAsPO();
                //LoadList();
                if (_warehouse == "admin")
                {
                    forPusat();
                }
                else
                {
                    forCabang();
                }
            }
        }
        public string warehouseCabang
        {
            get { return _warehouse; }
            set { _warehouse = value; }
        }
        public string ipCabang
        {
            get { return _ip; }
            set { _ip = value; }
        }
        private void updateVersion()
        {
            try
            {
                UpdateVersion _uv = new UpdateVersion();
                _uv.Update();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (_db.getConnection().State == ConnectionState.Open)
                {
                    _db.CloseConnection();
                }
            }
        }
        private void getDateAsPO()
        {
            string _date = DateTime.Now.ToString("yyyyMMdd");
            comboBox11.Text = _warehouse.ToUpper() + "/PO/" + _date;
        }
        private void forCabang()
        {
            tabControl1.TabPages.Remove(tabPage3);
            tabControl1.TabPages.Remove(tabPage6);
            tabControl1.TabPages.Remove(tabPage1);
            tabControl1.TabPages.Remove(tabPage2);
            tabControl1.TabPages.Remove(tabPage5);
        }
        private void forPusat()
        {
            /*tabControl1.TabPages.Remove(tabPage10);
            tabControl1.TabPages.Remove(tabPage1);
            tabControl1.TabPages.Remove(tabPage2);
            tabControl1.TabPages.Remove(tabPage5);

            //Khusus bwt Bizpark
            tabControl1.TabPages.Remove(tabPage4);
            tabControl1.TabPages.Remove(tabPage6);
            label9.Visible = false;
            comboBox8.Visible = false;
            label18.Visible = false;
            label19.Visible = false;
            label20.Visible = false;
            textBox17.Visible = false;
            textBox18.Visible = false;
            textBox19.Visible = false;*/
        }

        #region LoadList
        /*
         * Load semua data ke dalam UI, efeknya sama kyk refresh di web page
         */
        private void LoadList()
        {
            try
            {
                setTipeBarang();
                setSupplier();
                setLokasi();
                setCabang();
                setPONomor(false);
                setPOCabang(false);
                setNomorSuratJalan();
                setFinishedQueue();

                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                setLokasi(_command);
                showLokasi(_command);
                setTipeBarang(_command);
                showNamaBarang(_command);
                setWarnaSealBase(_command);
                showDaftarWarna(_command);
                setBarcode(_command);
                showBarcode(_command);
                showCabang(_command);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (_db.getConnection().State == ConnectionState.Open)
                {
                    _db.CloseConnection();
                }
            }
        }
        #endregion

        #region SQL Command
        /**
         * Bikin default dictionary value bwt mysql command
         * @param _detailbarang (apakah mysql yg diminta ada hubungannya dengan detail barang menggunakan barcode?)
         */
        private Dictionary<string, string> SQLDictionary()
        {
            Dictionary<string, string> _dictionary = new Dictionary<string, string> { { "select", "" }, { "distinct", "" }, { "from", "" }, { "detailbarang", "" }, { "where", "" }, { "orderby", "" }, { "update", "" }, { "set", "" }};
            return _dictionary;
        }
        /**
         * Transfer hasil mysql query ke dalam DataTable spy bisa di show ke user
         * @param _cmd (bwt open connection ke database)
         * @param _txt (string query perintah bwt jalanin query mysql)
         */
        private DataTable SQLCommand(MySqlCommand _cmd, string _txt)
        {
            _cmd.CommandText = _txt;
            DataSet _ds = new DataSet();
            MySqlDataAdapter _adap = new MySqlDataAdapter(_cmd);
            _adap.Fill(_ds);
            DataTable myDataTable = _ds.Tables[0];
            return myDataTable;
        }
        /**
         * Kode query universal bwt nyari apapun dari database
         * @param _cmd (bwt open connection ke database)
         * @param _order (dictionary array yg isinya perintah2 bwt gantiin universal code)
         */
        private String MySQLSelect(Dictionary<string, string> _order, Boolean _detailbarang)
        {
            if (_detailbarang == true)
            {
                _order["detailbarang"] = " INNER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode INNER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode ";
            }
            else
            {
                _order["detailbarang"] = "";
            }
            String _query = "SELECT " + _order["distinct"] + " " + _order["select"] + " FROM " + _order["from"] + _order["detailbarang"] + " " + _order["where"] + " " + _order["orderby"];
            return _query;
        }
        /**
         * Kode query universal bwt update apapun dari database
         * @param _cmd (bwt open connection ke database)
         * @param _order (dictionary array yg isinya perintah2 bwt gantiin universal code)
         */
        private String MySQLUpdate(Dictionary<string, string> _order)
        {
            String _query = "UPDATE " + _order["update"] + " SET " + _order["set"] + " " + _order["where"];
            return _query;
        }
        #endregion

        #region MySQL
        /**
         * SQL Query untuk ambil list nama barang
         * @param _cmd (bwt open connection ke database)
         * @param _tipe (tipe barang jadi atau barang sparepart atau lain2)
         * @param _search (apabila ada barang spesifik yg dicari @param ini akan berisi command)
         */
        private Array getNamaBarang(MySqlCommand _cmd, String _tipe, String _search)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Nama";
            _query["from"] = "list_barang";
            _query["where"] = "WHERE Tipe = '" + _tipe + "'" + _search;
            _query["orderby"] = "ORDER BY Nama";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Nama"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil distinct list tipe barang
         * @param _cmd (bwt open connection ke database)
         */
        private Array getTipeBarang(MySqlCommand _cmd)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["distinct"] = "DISTINCT";
            _query["select"] = "Tipe";
            _query["from"] = "list_barang";
            _query["orderby"] = "ORDER BY Tipe";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Tipe"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil semua nama supplier
         * @param _cmd (bwt open connection ke database)
         */
        private Array getSupplier(MySqlCommand _cmd)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Supplier";
            _query["from"] = "supplier";
            _query["where"] = "WHERE Supplier != ''";
            _query["orderby"] = "ORDER BY Supplier";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Supplier"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil semua nama lokasi
         * @param _cmd (bwt open connection ke database)
         */
        private Array getLokasi(MySqlCommand _cmd)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Lokasi";
            _query["from"] = "daftar_lokasi";
            _query["where"] = "WHERE Lokasi != ''";
            _query["orderby"] = "ORDER BY Lokasi";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Lokasi"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil semua nama cabang
         * @param _cmd (bwt open connection ke database)
         */
        private Array getCabang(MySqlCommand _cmd)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Cabang";
            _query["from"] = "cabang";
            _query["where"] = "WHERE Cabang != ''";
            _query["orderby"] = "ORDER BY Cabang";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Cabang"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil lokasi yg ada barangnya
         * @param _cmd (bwt open connection ke database)
         * @param _barcode (barcode barang yg ingin dicari lokasinya)
         */
        private Array getLokasiBarang(MySqlCommand _cmd, String _barcode)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Lokasi";
            _query["from"] = "lokasi_barang";
            _query["where"] = "WHERE Barang = '" + _barcode + "'";
            _query["orderby"] = "ORDER BY Lokasi";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Lokasi"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil jumlah barang yg ada di lokasi tersebut
         * @param _cmd (bwt open connection ke database)
         */
        private Array getStockInfoBarang(MySqlCommand _cmd, String _barcode, String _lokasi)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "(SELECT Jumlah FROM lokasi_barang WHERE Barang = '" + _barcode + "' AND lokasi_barang.Lokasi = '" + _lokasi + "') AS Jumlah, IF(SUM(Jumlah) IS NULL,0,SUM(Jumlah)) AS Queue";
            _query["from"] = "surat_jalan_detail INNER JOIN surat_jalan ON surat_jalan_detail.Nomor = surat_jalan.Nomor";
            _query["where"] = "WHERE surat_jalan_detail.Barang = '" + _barcode + "' AND surat_jalan_detail.Dari = '" + _lokasi + "' AND Queue = 0";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Jumlah"].ToString());
                _result.Add(_tempRow["Queue"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil barcode, nama, warna, dan total barang yg ada di stok semua lokasi
         * @param _cmd (bwt open connection ke database)
         * @param _item (nama barang yg dipilih untuk ditampilkan detailnya)
         */
        private DataTable getDetailBarang(MySqlCommand _cmd, String _item)
        {
            List<string> _result = new List<string>();
            List<List<string>> _result2 = new List<List<string>>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Barcode, Nama, daftar_warna.Warna, Total";
            _query["from"] = "barcode_barang LEFT JOIN (SELECT Barang, SUM(Jumlah) AS Total FROM lokasi_barang GROUP BY Barang) AS Total ON barcode_barang.Barcode = Total.Barang";
            _query["where"] = "WHERE list_barang.Nama = '" + _item + "'";
            _query["orderby"] = "ORDER BY Tipe";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, true));
            //code to convert datatable to list and then list to datatable
            /*DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Barcode"].ToString());
                _result.Add(_tempRow["Nama"].ToString());
                _result.Add(_tempRow["Warna"].ToString());
                _result.Add(_tempRow["Total"].ToString());
                _result2.Add(_result);
            }
            DataTable _dt = new DataTable();
            _dt.Columns.Add("Barcode", typeof(string));
            _dt.Columns.Add("Nama", typeof(string));
            _dt.Columns.Add("Warna", typeof(string));
            _dt.Columns.Add("Total", typeof(string));
            for (int i = 0; i < _array.Count; i++)
            {
                _dt.Rows.Add();
                _dt.Rows[i]["Barcode"] = _array[i][0 + (_dt.Columns.Count * i)];
                _dt.Rows[i]["Nama"] = _array[i][1 + (_dt.Columns.Count * i)];
                _dt.Rows[i]["Warna"] = _array[i][2 + (_dt.Columns.Count * i)];
                _dt.Rows[i]["Total"] = _array[i][3 + (_dt.Columns.Count * i)];
            }*/
            return _dt;
        }
        /**
         * SQL Query untuk ambil semua nomor PO dan percentage completionnya
         * @param _cmd (bwt open connection ke database)
         * @param _hutang (cek apakah checkbox hutang diceklist)
         */
        private DataTable getPONomor(MySqlCommand _cmd, Boolean _hutang, String _search)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "po.Cabang, po.PO, IF(po.Received = 1, 100, TRUNCATE(SUM(IF(po_detail.Jumlah<po_detail.Kirim,po_detail.Jumlah,po_detail.Kirim))/SUM(po_detail.Jumlah)*100,2) + '') AS `%`, IF(queue_info.Value IS NULL, 'Waiting for confirmation', queue_info.Value) AS Status, IF(po.Received = 1, 'Received', 'Pending') AS Received";
            _query["from"] = "po INNER JOIN po_detail ON po.PO = po_detail.PO LEFT JOIN warehouse_admin.surat_jalan_detail ON warehouse_admin.surat_jalan_detail.PO = po.PO LEFT JOIN warehouse_admin.surat_jalan ON warehouse_admin.surat_jalan.Nomor = warehouse_admin.surat_jalan_detail.Nomor LEFT JOIN queue_info ON warehouse_admin.surat_jalan.Queue = queue_info.Nomor";
            _query["orderby"] = "GROUP BY po.PO";
            _query["from"] = "(" + MySQLSelect(_query, false) + ") AS this";
            _query["select"] = "*";
            _query["where"] = "WHERE 1";
            if (_hutang == true)
            {
                _query["where"] += " AND this.`%` < 100 ";
            }
            _query["where"] += _search;
            _query["orderby"] = "ORDER BY this.PO";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            return _dt;
        }
        /**
         * SQL Query untuk ambil semua cabang PO dan percentage completionnya
         * @param _cmd (bwt open connection ke database)
         * @param _hutang (cek apakah checkbox hutang diceklist)
         */
        private DataTable getPOCabang(MySqlCommand _cmd, Boolean _hutang, String _search)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Cabang, IF(po.Received = 1, 100, TRUNCATE(SUM(IF(Jumlah<Kirim,Jumlah,Kirim))/SUM(Jumlah)*100,2) + '') AS `%`";
            _query["from"] = "po INNER JOIN po_detail ON po.PO = po_detail.PO";
            _query["orderby"] = "GROUP BY Cabang";
            _query["from"] = "(" + MySQLSelect(_query, false) + ") AS this";
            _query["select"] = "*";
            _query["where"] = "WHERE 1";
            if (_hutang == true)
            {
                _query["where"] += " AND this.`%` < 100 ";
            }
            _query["where"] += _search;
            _query["orderby"] = "ORDER BY this.Cabang";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            return _dt;
        }
        /**
         * SQL Query untuk ambil semua po_detail
         * @param _cmd (bwt open connection ke database)
         * @param _hutang (cek apakah checkbox hutang diceklist)
         * @param _where (kondisi WHERE untuk MySQL Query)
         */
        private DataTable getPODetail(MySqlCommand _cmd, Boolean _hutang, String _where, String _extra)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = _extra + "Cabang, po_detail.Barang AS Barcode, list_barang.Nama, daftar_warna.Warna";
            _query["from"] = "po_detail INNER JOIN po ON po_detail.PO = po.PO INNER JOIN barcode_barang ON po_detail.Barang = barcode_barang.Barcode";
            _query["where"] = _where;
            _query["orderby"] = "GROUP BY Barcode ORDER BY Cabang";
            if (_hutang == true)
            {
                _query["select"] += ", SUM(Jumlah - Kirim) AS 'PO Sisa'";
                _query["from"] = "(" + MySQLSelect(_query, true) + ") AS this";
                _query["select"] = "*";
                _query["where"] = "WHERE this.`PO Sisa` > 0";
                _query["orderby"] = "ORDER BY this.Cabang";
                _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            }
            else
            {
                _query["select"] += ", SUM(Jumlah) AS Jumlah, SUM(Kirim) AS Kirim";
                _dt = SQLCommand(_cmd, MySQLSelect(_query, true));
            }
            return _dt;
        }
        /**
         * SQL Query untuk ambil semua nomor surat jalan yg belum dikirim
         * @param _cmd (bwt open connection ke database)
         */
        private Array getNomorSuratJalan(MySqlCommand _cmd)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "Nomor";
            _query["from"] = "surat_jalan";
            _query["where"] = "WHERE Queue = 0";
            _query["orderby"] = "ORDER BY Nomor";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            DataRow _tempRow = null;
            foreach (DataRow _tempRow_Variable in _dt.Rows)
            {
                _tempRow = _tempRow_Variable;
                _result.Add(_tempRow["Nomor"].ToString());
            }
            var _array = _result.ToArray();
            return _array;
        }
        /**
         * SQL Query untuk ambil semua nomor surat jalan yg sudah dikirim
         * @param _cmd (bwt open connection ke database)
         */
        private DataTable getFinishedSuratJalan(MySqlCommand _cmd)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "surat_jalan.Nomor, Barang, Jumlah, Dari, Ke";
            _query["from"] = "surat_jalan INNER JOIN surat_jalan_detail ON surat_jalan.Nomor = surat_jalan_detail.Nomor";
            _query["where"] = "WHERE Queue > 0 AND Queue < 3";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, false));
            return _dt;
        }
        /**
         * SQL Query untuk ambil semua surat jalan yg ada di queue
         * @param _cmd (bwt open connection ke database)
         */
        private DataTable getSuratJalan(MySqlCommand _cmd, String _where)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "surat_jalan_detail.Nomor, PO, surat_jalan_detail.Barang AS Barcode, list_barang.Nama, daftar_warna.Warna, Jumlah, Dari, Ke, IF(Queue > (SELECT Nomor FROM queue_info WHERE Value = 'Confirmed'), 'Confirmed', queue_info.Value) AS Status, '' AS 'Check'";
            _query["from"] = "queue_info RIGHT JOIN surat_jalan ON queue_info.Nomor = surat_jalan.Queue INNER JOIN surat_jalan_detail ON surat_jalan.Nomor = surat_jalan_detail.Nomor INNER JOIN barcode_barang ON surat_jalan_detail.Barang = barcode_barang.Barcode";
            _query["where"] = "WHERE " + _where;
            _query["orderby"] = "ORDER BY surat_jalan_detail.Nomor";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, true));
            return _dt;
        }
        /**
         * SQL Query untuk ambil semua surat_jalan_detail
         * @param _cmd (bwt open connection ke database)
         * @param _nomor (nomor surat jalan yg ingin diambil detailnya)
         */
        private DataTable getSuratJalanDetail(MySqlCommand _cmd, String _nomor)
        {
            List<string> _result = new List<string>();
            DataTable _dt = new DataTable();
            Dictionary<string, string> _query = SQLDictionary();
            _query["select"] = "surat_jalan.Nomor, PO, surat_jalan_detail.Barang AS Barcode, list_barang.Nama, daftar_warna.Warna, Jumlah, Dari, Ke";
            _query["from"] = "surat_jalan INNER JOIN surat_jalan_detail ON surat_jalan.Nomor = surat_jalan_detail.Nomor INNER JOIN barcode_barang ON surat_jalan_detail.Barang = barcode_barang.Barcode";
            _query["where"] = "WHERE surat_jalan.Nomor = '" + _nomor + "'";
            _query["orderby"] = "ORDER BY surat_jalan_detail.Barang";
            _dt = SQLCommand(_cmd, MySQLSelect(_query, true));
            return _dt;
        }
        #endregion

        #region All Set
        /*
         * Method untuk set items di combobox tipe barang
         */
        private void setTipeBarang()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getTipeBarang(_command);
            _db.CloseConnection();
            comboBox12.Items.Clear();
            comboBox10.Items.Clear();
            comboBox20.Items.Clear();
            foreach (string i in _array)
            {
                comboBox12.Items.Add(i);
                comboBox10.Items.Add(i);
                comboBox20.Items.Add(i);
            }
            comboBox12.Text = "Pilih Tipe Barang...";
            comboBox10.Text = "Pilih Tipe Barang...";
            comboBox20.Text = "Pilih Tipe Barang...";
        }
        /*
         * Method untuk set semua nama supplier di combobox supplier
         */
        private void setSupplier()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getSupplier(_command);
            _db.CloseConnection();
            comboBox18.Items.Clear();
            foreach (string i in _array)
            {
                comboBox18.Items.Add(i);
            }
        }
        /*
         * Method untuk set semua nama lokasi di combobox lokasi
         */
        private void setLokasi()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getLokasi(_command);
            _db.CloseConnection();
            comboBox15.Items.Clear();
            comboBox9.Items.Clear();
            comboBox1.Items.Clear();
            foreach (string i in _array)
            {
                comboBox15.Items.Add(i);
                comboBox9.Items.Add(i);
                comboBox1.Items.Add(i);
            }
        }
        /*
         * Method untuk set semua nama cabang
         */
        private void setCabang()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getCabang(_command);
            _db.CloseConnection();
            comboBox7.Items.Clear();
            comboBox16.Items.Clear();
            foreach (string i in _array)
            {
                comboBox7.Items.Add(i);
                comboBox16.Items.Add(i);
            }
        }
        /*
         * Method untuk set nomor PO dan percentagenya di datagridview nomor PO
         */
        private void setPONomor(Boolean _check)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getPONomor(_command, _check, "");
            _db.CloseConnection();
            dataGridView11.DataSource = _dt;
            dataGridView17.DataSource = _dt;
            //comboBox11.DisplayMember = "PO";
            //comboBox11.DataSource = _dt;
        }
        /*
         * Method untuk set cabang dan percentage PO nya di datagridview cabang PO
         */
        private void setPOCabang(Boolean _check)
        {
            DataTable _dt;
            if (_warehouse != "admin")
            {
                DBConnect _db = new DBConnect(_ip, "warehouse_" + _warehouse);
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                _dt = getPOCabang(_command, _check, "");
                _db.CloseConnection();
            }
            else
            {
                List<string> _result1 = new List<string>();
                List<string> _result2 = new List<string>();
                _dt = new DataTable();
                _dt.Columns.Add("Cabang", typeof(string));
                _dt.Columns.Add("%", typeof(string));

                DBConnect _tempDB;
                string[] _cabang = new string[2] {"bizpark", "admin"};

                for (int c = 0; c < _cabang.Length; c++)
                {
                    _tempDB = new DBConnect(_ip, "warehouse_" + _cabang[c]);
                    MySqlCommand _command = _tempDB.OpenConnection().CreateCommand();
                    DataTable _dt2 = getPOCabang(_command, _check, "");
                    _tempDB.CloseConnection();

                    DataRow _tempRow = null;
                    foreach (DataRow _tempRow_Variable in _dt2.Rows)
                    {
                        _tempRow = _tempRow_Variable;
                        _result1.Add(_tempRow["Cabang"].ToString());
                        _result2.Add(_tempRow["%"].ToString());
                    }
                    var _array1 = _result1.ToArray();
                    var _array2 = _result2.ToArray();

                    for (int i = 0; i < _array1.Length; i++)
                    {
                        _dt.Rows.Add();
                        _dt.Rows[c]["Cabang"] = _array1[i];
                        _dt.Rows[c]["%"] = _array2[i];
                    }
                }
            }

            dataGridView14.DataSource = _dt;
            dataGridView18.DataSource = _dt;
        }
        /*
         * Method untuk set nomor surat jalan di combobox surat jalan
         */
        private void setNomorSuratJalan()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getNomorSuratJalan(_command);
            _db.CloseConnection();
            comboBox13.Items.Clear();
            comboBox14.Items.Clear();
            comboBox19.Items.Clear();
            foreach (string i in _array)
            {
                comboBox13.Items.Add(i);
                comboBox14.Items.Add(i);
                comboBox19.Items.Add(i);
            }
        }
        /*
         * Method untuk show queue list surat jalan
         */
        private void setFinishedQueue()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getFinishedSuratJalan(_command);
            _db.CloseConnection();
            dataGridView13.DataSource = _dt;
        }
        #endregion

        #region Halaman Penerimaan
        /*
         * ComboBox Tipe Barang
         */
        private void dataGridView8_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView8.SelectedRows;
            textBox29.Text = dataGridView8.Rows[_tempRow[0].Index].Cells["Nama"].Value.ToString();
        }
        private void comboBox12_SelectedIndexChanged(object sender, EventArgs e)
        {
            var _text = "AND Nama LIKE '" + textBox16.Text + "%'";
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            if (textBox16.Text == "Search..." || textBox16.Text == "") { _text = ""; }
            Array _array = getNamaBarang(_command, comboBox12.Text, _text);
            _db.CloseConnection();
            listBox2.Items.Clear();
            foreach (string i in _array)
            {
                listBox2.Items.Add(i);
            }
        }
        /*
         * ListBox Nama2 Barang
         */
        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            doTheListBox2();
        }
        private void doTheListBox2()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getDetailBarang(_command, listBox2.SelectedItem.ToString());
            _db.CloseConnection();
            dataGridView9.DataSource = _dt;
        }
        /*
         * DataGridView Detail Barang
         */
        private void dataGridView9_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView9.SelectedRows;
            if (dataGridView9.Rows[_tempRow[0].Index].Cells[0].Value == null)
            {
                button2.Enabled = false;
            }
            textBox1.Text = "";
            try
            {
                DataTable _dt = new DataTable();
                MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                _dt = SQLCommand(_cmd, "SELECT lokasi_barang.Lokasi,barcode_barang.Barcode,list_barang.Nama,daftar_warna.Warna,lokasi_barang.Jumlah FROM lokasi_barang LEFT OUTER JOIN barcode_barang ON barcode_barang.Barcode = lokasi_barang.Barang LEFT OUTER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode LEFT OUTER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode WHERE barcode_barang.Barcode = '" + dataGridView9.Rows[_tempRow[0].Index].Cells["Barcode"].Value + "' ORDER BY Lokasi");
                dataGridView10.DataSource = _dt;
                dataGridView8.DataSource = _dt;
                _db.CloseConnection();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /*
         * TextBox Jumlah Barang Masuk
         */
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView9.SelectedRows;
            if (textBox1.Text == "" || textBox1.Text == "0")
            {
                button2.Enabled = false;
            }
            else
            {
                button2.Enabled = true;
            }
        }
        /*
         * TextBox Jumlah Barang Keluar
         */
        private void textBox21_TextChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView9.SelectedRows;
            if (textBox21.Text == "" || textBox21.Text == "0")
            {
                button15.Enabled = false;
            }
            else
            {
                button15.Enabled = true;
            }
        }
        /*
         * Button OK Barang Keluar
         */
        private void button15_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> _query = SQLDictionary();
            MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
            _query["update"] = "lokasi_barang";
            _query["set"] = "Jumlah = Jumlah - " + textBox21.Text;
            _query["where"] = "WHERE Lokasi = '" + dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Lokasi"].Value.ToString() + "' AND Barang = '" + dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString() + "'";
            _cmd.CommandText = MySQLUpdate(_query);
            _cmd.Prepare();
            _cmd.ExecuteNonQuery();
            _cmd.CommandText = "INSERT INTO log_stok (barang, keluar, dari, ke) VALUES (@barang, @keluar, @dari, @ke)";
            _cmd.Prepare();
            _cmd.Parameters.AddWithValue("@barang", dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString());
            _cmd.Parameters.AddWithValue("@keluar", textBox21.Text);
            _cmd.Parameters.AddWithValue("@dari", dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Lokasi"].Value.ToString());
            _cmd.Parameters.AddWithValue("@ke", comboBox16.Text);
            _cmd.ExecuteNonQuery();
            showLokasi(_cmd, textBox29.Text);
            _db.CloseConnection();
            MessageBox.Show("Success");
        }
        /*
         * TextBox Jumlah Barang Pindah
         */
        private void textBox13_TextChanged(object sender, EventArgs e)
        {
            if (textBox13.Text != "")
            {
                button20.Enabled = true;
            }
            else
            {
                button20.Enabled = false;
            }
        }
        #endregion

        #region Halaman Insert PO
        /*
        * ComboBox Tipe Barang
        */
        private void comboBox10_SelectedIndexChanged(object sender, EventArgs e)
        {
            var _text = "AND Nama LIKE '" + textBox15.Text + "%'";
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            if (textBox15.Text == "Search..." || textBox15.Text == "") { _text = ""; }
            Array _array = getNamaBarang(_command, comboBox10.Text, _text);
            _db.CloseConnection();
            listBox3.Items.Clear();
            foreach (string i in _array)
            {
                listBox3.Items.Add(i);
            }
        }
        /*
        * ListBox Nama2 Barang
        */
        private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getDetailBarang(_command, listBox3.SelectedItem.ToString());
            _db.CloseConnection();
            dataGridView12.DataSource = _dt;
        }
        /*
        * DataGridView Detail Barang
        */
        private void dataGridView12_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView12.SelectedRows;
            if (dataGridView12.Rows[_tempRow[0].Index].Cells[0].Value == null)
            {
                button11.Enabled = false;
            }
            else
            {
                button11.Enabled = true;
            }
            textBox9.Text = "";
        }
        /*
        * TextBox jumlah untuk PO
        */
        private void textBox9_TextChanged(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView12.SelectedRows;
            if (textBox9.Text == "" || textBox9.Text == "0")
            {
                button11.Enabled = false;
            }
            else
            {
                button11.Enabled = true;
            }
        }
        /*
        * Button OK
        */
        private void button11_Click(object sender, EventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            insertPO(_command);
            _db.CloseConnection();
            setPONomor(checkBox2.Checked);
            textBox9.Text = "";
            MessageBox.Show("Success");
        }
        /*
        * Button Received
        */
        private void button39_Click(object sender, EventArgs e)
        {
            updateReceived(_warehouse, dataGridView17.Rows[dataGridView17.CurrentCellAddress.Y].Cells["PO"].Value.ToString());
            setPONomor(checkBox2.Checked);
        }
        /*
        * CheckBox Hutang
        */
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            setPONomor(checkBox2.Checked);
            setPOCabang(checkBox2.Checked);
        }
        /*
        * DataGridView No. PO
        */
        private void dataGridView17_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getPODetail(_command, checkBox1.Checked, "WHERE po_detail.PO = '" + dataGridView17.Rows[e.RowIndex].Cells["PO"].Value.ToString() + "'", "po.PO, ");
            dataGridView19.DataSource = _dt;
            _db.CloseConnection();
        }
        /*
        * DataGridView Cabang PO
        */
        private void dataGridView18_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getPODetail(_command, checkBox1.Checked, "WHERE po.Cabang = '" + dataGridView18.Rows[e.RowIndex].Cells["Cabang"].Value.ToString() + "'", "");
            dataGridView19.DataSource = _dt;
            _db.CloseConnection();
        }
        #endregion

        #region Halaman Pengiriman
        /*
         * CheckBox Hutang
         */
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            setPONomor(checkBox1.Checked);
            setPOCabang(checkBox1.Checked);
        }
        /*
         * DataGridView No. PO
         */
        private void dataGridView11_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DBConnect _dbCabang = new DBConnect(_ip, "warehouse_" + dataGridView14.Rows[dataGridView14.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString().ToLower());
            MySqlCommand _command = _dbCabang.OpenConnection().CreateCommand();
            DataTable _dt = getPODetail(_command, checkBox1.Checked, "WHERE po_detail.PO = '" + dataGridView11.Rows[e.RowIndex].Cells["PO"].Value.ToString() + "'", "po.PO, ");
            dataGridView1.DataSource = _dt;
            _dbCabang.CloseConnection();
        }
        /*
         * DataGridView Cabang PO
         */
        private void dataGridView14_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DBConnect _dbCabang = new DBConnect(_ip, "warehouse_" + dataGridView14.Rows[e.RowIndex].Cells["Cabang"].Value.ToString().ToLower());
            MySqlCommand _command = _dbCabang.OpenConnection().CreateCommand();
            DataTable _dt = getPODetail(_command, checkBox1.Checked, "WHERE po.Cabang = '" + dataGridView14.Rows[e.RowIndex].Cells["Cabang"].Value.ToString() + "'", "");
            dataGridView1.DataSource = _dt;
            _dt = getPONomor(_command, checkBox1.Checked, "");
            _db.CloseConnection();
            dataGridView11.DataSource = _dt;
            //comboBox11.DisplayMember = "PO";
            //comboBox11.DataSource = _dt;
            _dbCabang.CloseConnection();
        }
        /*
         * DataGridView Detail PO
         */
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Rows[e.RowIndex].Cells[0].Value == null)
            {
                button11.Enabled = false;
            }
            else
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                Array _array = getLokasiBarang(_command, dataGridView1.Rows[e.RowIndex].Cells["Barcode"].Value.ToString());
                _db.CloseConnection();
                comboBox8.Items.Clear();
                comboBox21.Items.Clear();
                foreach (string i in _array)
                {
                    comboBox8.Items.Add(i);
                    comboBox21.Items.Add(i);
                }
            }
            textBox8.Text = "";
            textBox17.Text = "";
            textBox18.Text = "";
            textBox19.Text = "";
            textBox28.Text = "";
            textBox27.Text = "";
            textBox26.Text = "";
            textBox23.Text = "";
        }
        /*
         * ComboBox lokasi untuk surat jalan
         */
        private void comboBox8_SelectedIndexChanged(object sender, EventArgs e)
        {
            doTheComboBox8();
        }
        private void doTheComboBox8()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getStockInfoBarang(_command, dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString(), comboBox8.Text);
            _db.CloseConnection();
            textBox17.Text = _array.GetValue(0).ToString();
            textBox18.Text = _array.GetValue(1).ToString();
            if (textBox17.Text == "") { textBox17.Text = "0"; }
            if (textBox18.Text == "") { textBox18.Text = "0"; }
            textBox19.Text = (Int32.Parse(textBox17.Text) - Int32.Parse(textBox18.Text)).ToString();
            _db.CloseConnection();
        }
        /*
         * TextBox jumlah untuk surat jalan
         */
        private void textBox8_TextChanged(object sender, EventArgs e)
        {
            if (textBox8.Text == "" || textBox8.Text == "0")
            {
                button10.Enabled = false;
            }
            else
            {
                button10.Enabled = true;
            }
        }
        /*
         * ComboBox nomor surat jalan
         */
        private void comboBox13_TextChanged(object sender, EventArgs e)
        {
            doTheComboBox13();
        }
        private void doTheComboBox13()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getSuratJalanDetail(_command, ActiveControl.Text);
            _db.CloseConnection();
            dataGridView2.DataSource = _dt;
        }
        /*
         * Button OK untuk barang surat jalan
         */
        private void button10_Click(object sender, EventArgs e)
        {
            var _nomor = comboBox13.Text;
            var _barang = dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString();
            var _jumlah = Int32.Parse(textBox8.Text);
            var _dari = comboBox8.Text;
            var _ke = dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString();
            string _po = "";
            try
            {
                _po = dataGridView1.Rows[0].Cells["PO"].Value.ToString();
            }
            catch (Exception exc)
            {
                DBConnect _dbCabang = new DBConnect(_ip, "warehouse_" + dataGridView14.Rows[dataGridView14.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString().ToLower());
                MySqlCommand _cmd = _dbCabang.OpenConnection().CreateCommand();
                Dictionary<string, string> _query = SQLDictionary();
                _query["select"] = "po.PO";
                _query["from"] = "po INNER JOIN po_detail ON po.PO = po_detail.PO";
                _query["where"] = "WHERE Cabang = '" + _ke + "' AND Barang = '" + _barang + "'";
                _query["orderby"] = "ORDER BY po.PO LIMIT 0,1";
                DataTable _tempDt = SQLCommand(_cmd, MySQLSelect(_query, false));
                DataRow _tempRow = null;
                foreach (DataRow tempRow_Variable in _tempDt.Rows)
                {
                    _tempRow = tempRow_Variable;
                    _po = _tempRow["PO"].ToString();
                }
                _dbCabang.CloseConnection();
            }
            try
            {
                
                MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                _cmd.CommandText = "INSERT IGNORE surat_jalan (Nomor, Ke) VALUES (@Nomor, @Ke)";
                _cmd.Prepare();
                _cmd.Parameters.AddWithValue("@Nomor", _nomor);
                _cmd.Parameters.AddWithValue("@Ke", _ke);
                _cmd.ExecuteNonQuery();
                Dictionary<string, string> _query = SQLDictionary();
                _query["select"] = "*";
                _query["from"] = "surat_jalan_detail";
                _query["where"] = "WHERE Nomor = '" + _nomor + "' AND Barang = '" + _barang + "' AND Dari = '" + _dari + "' AND PO = '" + _po + "'";
                if (SQLCommand(_cmd, MySQLSelect(_query, false)).Rows.Count >= 1)
                {
                    _query["update"] = "surat_jalan_detail";
                    _query["set"] = "Jumlah = Jumlah + " + _jumlah;
                    _cmd.CommandText = MySQLUpdate(_query);
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
                else
                {
                    _cmd.CommandText = "INSERT INTO surat_jalan_detail (Nomor, Barang, Jumlah, Dari, PO) VALUES (@Nomor, @Barang, @Jumlah, @Dari, @PO)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Barang", _barang);
                    _cmd.Parameters.AddWithValue("@Jumlah", _jumlah);
                    _cmd.Parameters.AddWithValue("@Dari", _dari);
                    _cmd.Parameters.AddWithValue("@PO", _po);
                    _cmd.ExecuteNonQuery();
                }
                DBConnect _dbCabang = new DBConnect(_ip, "warehouse_" + dataGridView14.Rows[dataGridView14.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString().ToLower());
                MySqlCommand _cmdCabang = _dbCabang.OpenConnection().CreateCommand();
                Dictionary<string, string> _queryCabang = SQLDictionary();
                _queryCabang["update"] = "po_detail";
                _queryCabang["set"] = "Kirim = Kirim + " + _jumlah;
                _queryCabang["where"] = "WHERE PO = '" + _po + "' AND Barang = '" + _barang + "'";
                _cmdCabang.CommandText = MySQLUpdate(_queryCabang);
                _cmdCabang.Prepare();
                _cmdCabang.ExecuteNonQuery();
                _dbCabang.CloseConnection();
                DataTable _dt = getSuratJalanDetail(_cmd, comboBox13.Text);
                _db.CloseConnection();
                dataGridView2.DataSource = _dt;
                textBox8.Text = "";
                doTheComboBox8();
                MessageBox.Show("Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "Failed to insert data surat jalan");
            }
        }
        /*
        * Button Received
        */
        private void button33_Click(object sender, EventArgs e)
        {
            updateReceived(dataGridView11.Rows[dataGridView11.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString().ToLower(), dataGridView11.Rows[dataGridView11.CurrentCellAddress.Y].Cells["PO"].Value.ToString());
            setPONomor(checkBox2.Checked);
        }
        /*
         * Button Print
         */
        private void button12_Click(object sender, EventArgs e)
        {
            String _toPrint = comboBox13.Text;
            using (Form2 form2 = new Form2(_toPrint, _ip, dataGridView2.Rows[dataGridView2.CurrentCellAddress.Y].Cells["Ke"].Value.ToString()))
            {
                form2.ShowDialog();
            }
        }
        #endregion

        #region Halaman Queue
        /*
         * ComboBox nomor surat jalan
         */
        private void comboBox14_TextChanged(object sender, EventArgs e)
        {
            doTheComboBox14();
        }
        private void doTheComboBox14()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getSuratJalan(_command, "surat_jalan_detail.Nomor = '" + ActiveControl.Text + "'");
            _db.CloseConnection();
            dataGridView15.DataSource = _dt;
        }
        /*
         * Button Check
         */
        private void button37_Click(object sender, EventArgs e)
        {
            dataGridView15.Rows[dataGridView15.CurrentCellAddress.Y].Cells["Check"].Value = "Checked";
        }
        /*
         * Button Check All
         */
        private void button38_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView15.Rows.Count - 1; i++)
            {
                dataGridView15.Rows[i].Cells["Check"].Value = "Checked";
            }
        }
        /*
         * Button Send
         */
        private void button32_Click(object sender, EventArgs e)
        {
            bool _confirm = true;
            for (int i = 0; i < dataGridView15.Rows.Count - 1; i++)
            {
                if (dataGridView15.Rows[i].Cells["Check"].Value.ToString() != "Checked")
                {
                    _confirm = false;
                }
            }
            if (_confirm == true)
            {
                Dictionary<string, string> _query = SQLDictionary();
                MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                _cmd.CommandText = "UPDATE surat_jalan SET Queue = Queue + 1 WHERE Nomor = '" + comboBox14.Text + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
                DBConnect _tempDB;
                for (int i = 0; i < dataGridView15.Rows.Count - 1; i++)
                {
                    _tempDB = new DBConnect(_ip, "warehouse_" + _warehouse);
                    MySqlCommand _command2 = _tempDB.OpenConnection().CreateCommand();
                    _command2.CommandText = "UPDATE lokasi_barang SET Jumlah = Jumlah - " + dataGridView15.Rows[i].Cells["Jumlah"].Value.ToString() + " WHERE Barang = '" + dataGridView15.Rows[i].Cells["Barcode"].Value.ToString() + "' AND Lokasi = '" + dataGridView15.Rows[i].Cells["Dari"].Value.ToString() + "'";
                    _command2.Prepare();
                    _command2.ExecuteNonQuery();
                    _tempDB.CloseConnection();
                    _tempDB = new DBConnect(_ip, "warehouse_" + "admin");
                    MySqlCommand _command1 = _tempDB.OpenConnection().CreateCommand();
                    _command1.CommandText = "INSERT INTO log_stok (barang, keluar, dari, ke) VALUES (@barang, @keluar, @dari, @ke)";
                    _command1.Prepare();
                    _command1.Parameters.AddWithValue("@barang", dataGridView15.Rows[i].Cells["Barcode"].Value.ToString());
                    _command1.Parameters.AddWithValue("@keluar", dataGridView15.Rows[i].Cells["Jumlah"].Value.ToString());
                    _command1.Parameters.AddWithValue("@dari", dataGridView15.Rows[i].Cells["Dari"].Value.ToString());
                    _command1.Parameters.AddWithValue("@ke", dataGridView15.Rows[i].Cells["Ke"].Value.ToString());
                    _command1.ExecuteNonQuery();
                    _tempDB.CloseConnection();
                    /*
                    _tempDB = new DBConnect("warehouse_" + dataGridView15.Rows[i].Cells["Ke"].Value.ToString());
                    MySqlCommand _command2 = _tempDB.OpenConnection().CreateCommand();
                    _query["update"] = "po_detail";
                    _query["set"] = "Kirim = Kirim + " + dataGridView15.Rows[i].Cells["Jumlah"].Value.ToString();
                    _query["where"] = "WHERE PO = '" + dataGridView15.Rows[i].Cells["PO"].Value.ToString() + "' AND Barang = '" + dataGridView15.Rows[i].Cells["Barcode"].Value.ToString() + "'";
                    _command2.CommandText = MySQLUpdate(_query);
                    _command2.Prepare();
                    _command2.ExecuteNonQuery();
                    _query["update"] = "po";
                    _query["set"] = "Received = 0";
                    _query["where"] = "WHERE PO = '" + dataGridView15.Rows[i].Cells["PO"].Value.ToString() + "'";
                    _command2.CommandText = MySQLUpdate(_query);
                    _command2.Prepare();
                    _command2.ExecuteNonQuery();
                    _tempDB.CloseConnection();*/
                }
                _cmd.CommandText = "UPDATE surat_jalan SET Queue = Queue + 1 WHERE Nomor = '" + comboBox14.Text + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();

                _db.CloseConnection();
                doTheComboBox14();
                setFinishedQueue();
                MessageBox.Show("Success");
            }
            else
            {
                MessageBox.Show("Belum di check semua");
            }
        }
        /*
         * Button Confirmed
         */
        private void button40_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> _query = SQLDictionary();
            MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
            _cmd.CommandText = "UPDATE surat_jalan SET Queue = Queue + 1 WHERE Nomor = '" + dataGridView13.Rows[dataGridView13.CurrentCellAddress.Y].Cells["Nomor"].Value.ToString() + "'";
            _cmd.Prepare();
            _cmd.ExecuteNonQuery();
            _db.CloseConnection();
            setFinishedQueue();
        }
        #endregion

        #region Halaman Pengiriman Sementara
        /*
         * ComboBox Tipe Barang
         */
        private void comboBox20_SelectedIndexChanged(object sender, EventArgs e)
        {
            var _text = "AND Nama LIKE '" + textBox24.Text + "%'";
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            if (textBox24.Text == "Search..." || textBox24.Text == "") { _text = ""; }
            Array _array = getNamaBarang(_command, comboBox20.Text, _text);
            _db.CloseConnection();
            listBox4.Items.Clear();
            foreach (string i in _array)
            {
                listBox4.Items.Add(i);
            }
        }
        /*
         * ListBox Nama2 Barang
         */
        private void listBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            doTheListBox4();
        }
        private void doTheListBox4()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getDetailBarang(_command, listBox4.SelectedItem.ToString());
            _db.CloseConnection();
            dataGridView21.DataSource = _dt;
        }
        /*
         * DataGridView Detail Barang
         */
        private void dataGridView21_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView21.SelectedRows;
            if (dataGridView21.Rows[_tempRow[0].Index].Cells[0].Value == null)
            {
                button43.Enabled = false;
            }
            else
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                Array _array = getLokasiBarang(_command, dataGridView21.Rows[e.RowIndex].Cells["Barcode"].Value.ToString());
                _db.CloseConnection();
                comboBox21.Items.Clear();
                foreach (string i in _array)
                {
                    comboBox21.Items.Add(i);
                }
            }
            textBox23.Text = "";
        }
        /*
         * ComboBox lokasi untuk surat jalan
         */
        private void comboBox21_SelectedIndexChanged(object sender, EventArgs e)
        {
            doTheComboBox21();
        }
        private void doTheComboBox21()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            Array _array = getStockInfoBarang(_command, dataGridView21.Rows[dataGridView21.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString(), comboBox21.Text);
            _db.CloseConnection();
            textBox28.Text = _array.GetValue(0).ToString();
            textBox27.Text = _array.GetValue(1).ToString();
            if (textBox28.Text == "") { textBox28.Text = "0"; }
            if (textBox27.Text == "") { textBox27.Text = "0"; }
            textBox26.Text = (Int32.Parse(textBox28.Text) - Int32.Parse(textBox27.Text)).ToString();
            _db.CloseConnection();
        }
        /*
         * TextBox jumlah untuk surat jalan
         */
        private void textBox23_TextChanged(object sender, EventArgs e)
        {
            if (textBox23.Text == "" || textBox23.Text == "0")
            {
                button43.Enabled = false;
            }
            else
            {
                button43.Enabled = true;
            }
        }
        /*
         * ComboBox nomor surat jalan
         */
        private void comboBox19_TextChanged(object sender, EventArgs e)
        {
            doTheComboBox19();
        }
        private void doTheComboBox19()
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            DataTable _dt = getSuratJalanDetail(_command, ActiveControl.Text);
            _db.CloseConnection();
            dataGridView20.DataSource = _dt;
        }
        /*
         * Button bikin surat jalan
         */
        private void button43_Click(object sender, EventArgs e)
        {
            var _nomor = comboBox19.Text;
            var _barang = dataGridView21.Rows[dataGridView21.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString();
            var _jumlah = Int32.Parse(textBox23.Text);
            var _dari = comboBox21.Text;
            var _ke = textBox25.Text;
            if (_ke == null)
            {
                _ke = "";
            }
            try
            {

                MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                _cmd.CommandText = "INSERT IGNORE surat_jalan (Nomor, Ke) VALUES (@Nomor, @Ke)";
                _cmd.Prepare();
                _cmd.Parameters.AddWithValue("@Nomor", _nomor);
                _cmd.Parameters.AddWithValue("@Ke", _ke);
                _cmd.ExecuteNonQuery();
                Dictionary<string, string> _query = SQLDictionary();
                _query["select"] = "*";
                _query["from"] = "surat_jalan_detail";
                _query["where"] = "WHERE Nomor = '" + _nomor + "' AND Barang = '" + _barang + "' AND Dari = '" + _dari + "'";
                if (SQLCommand(_cmd, MySQLSelect(_query, false)).Rows.Count >= 1)
                {
                    _query["update"] = "surat_jalan_detail";
                    _query["set"] = "Jumlah = Jumlah + " + _jumlah;
                    _cmd.CommandText = MySQLUpdate(_query);
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
                else
                {
                    _cmd.CommandText = "INSERT INTO surat_jalan_detail (Nomor, Barang, Jumlah, Dari, PO) VALUES (@Nomor, @Barang, @Jumlah, @Dari, @PO)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Barang", _barang);
                    _cmd.Parameters.AddWithValue("@Jumlah", _jumlah);
                    _cmd.Parameters.AddWithValue("@Dari", _dari);
                    _cmd.Parameters.AddWithValue("@PO", "");
                    _cmd.ExecuteNonQuery();
                }
                DataTable _dt = getSuratJalanDetail(_cmd, comboBox19.Text);
                _db.CloseConnection();
                dataGridView20.DataSource = _dt;
                textBox23.Text = "";
                doTheComboBox21();
                MessageBox.Show("Success");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString() + "Failed to insert data surat jalan");
            }
        }
        /*
         * Button Print Langsung
         */
        private void button42_Click(object sender, EventArgs e)
        {
            String _toPrint = comboBox19.Text;
            using (Form2 form2 = new Form2(_toPrint, _ip, _warehouse))
            {
                form2.ShowDialog();
            }
        }
        /*
         * Button Export To Excel
         */
        private void button45_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Excel Documents (*.xls)|*.xls";
            sfd.FileName = "surat_jalan_nomor_" + comboBox19.Text + ".xls";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //ToCsV(dataGridView1, @"c:\export.xls");
                Excel _excel = new Excel();
                _excel.ToCsV(dataGridView20, sfd.FileName); // Here dataGridview1 is your grid view name 
            }
        }
        #endregion

        #region Listener and Restriction for user input
        /*
         * Listener untuk limit textbox hanya bisa diisi oleh number
         */
        private void limitNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)Keys.Back)))
                e.Handled = true;
        }
        /*
         * Listener searchbox apabila siap di ketik
         */
        private void searchBox_Enter(object sender, EventArgs e)
        {
            if (this.ActiveControl.Text == "Search...") { this.ActiveControl.Text = ""; }
        }
        /*
         * Listener sesudah meninggalkan searchbox
         */
        private void searchBox_Leave(object sender, EventArgs e)
        {
            if (textBox16.Text == "") { textBox16.Text = "Search..."; }
            if (textBox3.Text == "") { textBox3.Text = "Search..."; }
            if (textBox15.Text == "") { textBox15.Text = "Search..."; }
            if (textBox20.Text == "") { textBox20.Text = "Search..."; }
            if (textBox24.Text == "") { textBox24.Text = "Search..."; }
            if (textBox29.Text == "") { textBox29.Text = "Search..."; }
        }
        /*
         * Listener apabila user menekan tombol keyboard ketika menggunakan searchbox
         */
        private void searchBoxListener_KeyUp(object sender, KeyEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            if (this.ActiveControl.Name == "textBox16")
            {
                var _text = " AND Nama LIKE '" + textBox16.Text + "%'";
                if (textBox16.Text == "Search..." || textBox16.Text == "") { _text = ""; }
                Array _array = getNamaBarang(_command, comboBox12.Text, _text);
                listBox2.Items.Clear();
                foreach (string i in _array)
                {
                    listBox2.Items.Add(i);
                }
            }
            else if (this.ActiveControl.Name == "textBox3")
            {
                var _text = " AND this.PO LIKE '" + textBox3.Text + "%'";
                DataTable _dt = getPONomor(_command, checkBox1.Checked, _text);
                dataGridView11.DataSource = _dt;
                _text = " AND this.Cabang LIKE '" + textBox3.Text + "%'";
                _dt = getPOCabang(_command, checkBox1.Checked, _text);
                dataGridView14.DataSource = _dt;
            }
            else if (this.ActiveControl.Name == "textBox15")
            {
                var _text = " AND Nama LIKE '" + textBox15.Text + "%'";
                if (textBox15.Text == "Search..." || textBox15.Text == "") { _text = ""; }
                Array _array = getNamaBarang(_command, comboBox10.Text, _text);
                listBox3.Items.Clear();
                foreach (string i in _array)
                {
                    listBox3.Items.Add(i);
                }
            }
            else if (this.ActiveControl.Name == "textBox20")
            {
                var _text = " AND this.PO LIKE '" + textBox20.Text + "%'";
                DataTable _dt = getPONomor(_command, checkBox2.Checked, _text);
                dataGridView17.DataSource = _dt;
                _text = " AND this.Cabang LIKE '" + textBox20.Text + "%'";
                _dt = getPOCabang(_command, checkBox2.Checked, _text);
                dataGridView18.DataSource = _dt;
            }
            else if (this.ActiveControl.Name == "textBox24")
            {
                var _text = " AND Nama LIKE '" + textBox24.Text + "%'";
                if (textBox24.Text == "Search..." || textBox24.Text == "") { _text = ""; }
                Array _array = getNamaBarang(_command, comboBox20.Text, _text);
                listBox4.Items.Clear();
                foreach (string i in _array)
                {
                    listBox4.Items.Add(i);
                }
            }
            else if (this.ActiveControl.Name == "textBox29")
            {
                var _text = " WHERE Nama LIKE '" + textBox29.Text + "%'";
                if (textBox29.Text == "Search..." || textBox29.Text == "") { _text = ""; }
                try
                {
                    DataTable _dt = new DataTable();
                    _dt = SQLCommand(_command, "SELECT lokasi_barang.Lokasi,barcode_barang.Barcode,list_barang.Nama,daftar_warna.Warna,lokasi_barang.Jumlah FROM lokasi_barang LEFT OUTER JOIN barcode_barang ON barcode_barang.Barcode = lokasi_barang.Barang LEFT OUTER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode LEFT OUTER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode " + _text + " ORDER BY Lokasi");
                    dataGridView8.DataSource = _dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (this.ActiveControl.Name == "textBox30")
            {
                var _text = " WHERE Nama LIKE '" + textBox30.Text + "%'";
                if (textBox30.Text == "Search..." || textBox30.Text == "") { _text = ""; }
                try
                {
                    DataTable _dt = new DataTable();
                    _dt = SQLCommand(_command, "SELECT * FROM list_barang " + _text + " ORDER BY Tipe");
                    dataGridView3.DataSource = _dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if (this.ActiveControl.Name == "textBox31")
            {
                showDaftarWarna(_command);
            }
            else if (this.ActiveControl.Name == "textBox32")
            {
                showBarcode(_command);
            }
            _db.CloseConnection();
        }
        /*
         * Listener untuk copy content datagridview yg dipilih ke dalam textbox/combobox
         */
        private void copyContent_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.ActiveControl.Name == "dataGridView3")
            {
                textBox4.Text = dataGridView3.Rows[e.RowIndex].Cells["Kode"].Value.ToString();
                textBox6.Text = dataGridView3.Rows[e.RowIndex].Cells["Nama"].Value.ToString();
                comboBox2.Text = dataGridView3.Rows[e.RowIndex].Cells["Tipe"].Value.ToString();
                textBox30.Text = dataGridView3.Rows[e.RowIndex].Cells["Nama"].Value.ToString();
            }
            else if (this.ActiveControl.Name == "dataGridView4")
            {
                textBox10.Text = dataGridView4.Rows[e.RowIndex].Cells["Kode"].Value.ToString();
                textBox7.Text = dataGridView4.Rows[e.RowIndex].Cells["Warna"].Value.ToString();
                comboBox3.Text = dataGridView4.Rows[e.RowIndex].Cells["Seal"].Value.ToString();
                comboBox4.Text = dataGridView4.Rows[e.RowIndex].Cells["Base"].Value.ToString();
                textBox11.Text = dataGridView4.Rows[e.RowIndex].Cells["Info"].Value.ToString();
                textBox31.Text = dataGridView4.Rows[e.RowIndex].Cells["Warna"].Value.ToString();
            }
            else if (this.ActiveControl.Name == "dataGridView6")
            {
                textBox14.Text = dataGridView6.Rows[e.RowIndex].Cells["Barcode"].Value.ToString();
                comboBox6.Text = dataGridView6.Rows[e.RowIndex].Cells["Nama"].Value.ToString();
                comboBox5.Text = dataGridView6.Rows[e.RowIndex].Cells["Warna1"].Value.ToString();
                textBox32.Text = dataGridView6.Rows[e.RowIndex].Cells["Nama"].Value.ToString();
            }
        }

        #region General Button
        /*
         * Button untuk refresh semua halaman
         */
        private void refreshButton_Click(object sender, EventArgs e)
        {
            LoadList();
        }
        /*
         * Button untuk delete di semua halaman
         */
        private void deleteButton_Click(object sender, EventArgs e)
        {
            /*
             * Halaman Penerimaan
             */
            if (this.ActiveControl.Name == "button19")
            {
                var _lokasi = dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Lokasi"].Value;
                var _barang = dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Barcode"].Value;
                var _jumlah = dataGridView8.Rows[dataGridView8.CurrentCellAddress.Y].Cells["Jumlah"].Value;
                if (MessageBox.Show("Are you sure you want to delete " + _jumlah + " " + _barang + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM lokasi_barang WHERE Lokasi = '" + _lokasi + "' AND Barang = '" + _barang + "' AND Jumlah = '" + _jumlah + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    showLokasi(_cmd);
                    _db.CloseConnection();
                }
            }
            /*
             * End of Halaman Penerimaan
             */
            /*
             * Halaman Pengiriman
             */
            else if (this.ActiveControl.Name == "button29")
            {
                if (MessageBox.Show("Are you sure you want to delete PO " + dataGridView11.Rows[dataGridView11.CurrentCellAddress.Y].Cells["PO"].Value.ToString() + " and all of its details?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM po WHERE PO = '" + dataGridView11.Rows[dataGridView11.CurrentCellAddress.Y].Cells["PO"].Value.ToString() + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _db.CloseConnection();
                    setPONomor(checkBox1.Checked);
                }
            }
            else if (this.ActiveControl.Name == "button31")
            {
                if (MessageBox.Show("Are you sure you want to delete all PO from Cabang " + dataGridView14.Rows[dataGridView14.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString() + " and all of its details?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM po WHERE Cabang = '" + dataGridView14.Rows[dataGridView14.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString() + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _db.CloseConnection();
                    setPOCabang(checkBox1.Checked);
                }
            }
            else if (this.ActiveControl.Name == "button16")
            {
                if (MessageBox.Show("Are you sure you want to delete this surat jalan?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    var _nomor = dataGridView2.Rows[dataGridView2.CurrentCellAddress.Y].Cells["Nomor"].Value.ToString();
                    var _barcode = dataGridView2.Rows[dataGridView2.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString();
                    var _dari = dataGridView2.Rows[dataGridView2.CurrentCellAddress.Y].Cells["Dari"].Value.ToString();
                    var _po = dataGridView2.Rows[dataGridView2.CurrentCellAddress.Y].Cells["PO"].Value.ToString();
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM surat_jalan_detail WHERE Nomor = '" + _nomor + "' AND Barang = '" + _barcode + "' AND Dari = '" + _dari + "' AND PO = '" + _po + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _cmd.CommandText = "DELETE FROM surat_jalan WHERE Nomor NOT IN (SELECT Nomor FROM surat_jalan_detail)";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _db.CloseConnection();
                    doTheComboBox13();
                }
            }
            /*
             * End of Halaman Pengiriman
             */
            /*
             * Halaman Insert PO
             */
            else if (this.ActiveControl.Name == "button34")
            {
                if (MessageBox.Show("Are you sure you want to delete PO " + dataGridView17.Rows[dataGridView17.CurrentCellAddress.Y].Cells["PO"].Value.ToString() + " and all of its details?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM po WHERE PO = '" + dataGridView17.Rows[dataGridView17.CurrentCellAddress.Y].Cells["PO"].Value.ToString() + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _db.CloseConnection();
                    setPONomor(checkBox2.Checked);
                }
            }
            else if (this.ActiveControl.Name == "button35")
            {
                if (MessageBox.Show("Are you sure you want to delete all PO from Cabang " + dataGridView18.Rows[dataGridView18.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString() + " and all of its details?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM po WHERE Cabang = '" + dataGridView18.Rows[dataGridView18.CurrentCellAddress.Y].Cells["Cabang"].Value.ToString() + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _db.CloseConnection();
                    setPOCabang(checkBox2.Checked);
                }
            }
            /*
             * End of Halaman Insert PO
             */
            /*
             * Halaman Pengiriman Sementara
             */
            else if (this.ActiveControl.Name == "button41")
            {
                if (MessageBox.Show("Are you sure you want to delete this surat jalan?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    var _nomor = dataGridView20.Rows[dataGridView20.CurrentCellAddress.Y].Cells["Nomor"].Value.ToString();
                    var _barcode = dataGridView20.Rows[dataGridView20.CurrentCellAddress.Y].Cells["Barcode"].Value.ToString();
                    var _dari = dataGridView20.Rows[dataGridView20.CurrentCellAddress.Y].Cells["Dari"].Value.ToString();
                    var _po = dataGridView20.Rows[dataGridView20.CurrentCellAddress.Y].Cells["PO"].Value.ToString();
                    MySqlCommand _cmd = _db.OpenConnection().CreateCommand();
                    _cmd.CommandText = "DELETE FROM surat_jalan_detail WHERE Nomor = '" + _nomor + "' AND Barang = '" + _barcode + "' AND Dari = '" + _dari + "' AND PO = '" + _po + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _cmd.CommandText = "DELETE FROM surat_jalan WHERE Nomor NOT IN (SELECT Nomor FROM surat_jalan_detail)";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                    _db.CloseConnection();
                    doTheComboBox19();
                }
            }
            /*
             * End of Halaman Pengiriman Sementara
             */
        }
        #endregion
        #endregion











        /*
         * Kode bwt update status received di table po di database cabang
         */
        private void updateReceived(String _cabang, String _po)
        {
            Dictionary<string, string> _query = SQLDictionary();
            DBConnect _tempDB = new DBConnect(_ip, "warehouse_" + _cabang);
            MySqlCommand _cmd = _tempDB.OpenConnection().CreateCommand();
            _query["update"] = "po";
            _query["set"] = "Received = 1";
            _query["where"] = "WHERE PO = '" + _po + "'";
            _cmd.CommandText = MySQLUpdate(_query);
            _cmd.Prepare();
            _cmd.ExecuteNonQuery();
            _tempDB.CloseConnection();
        }



        /* 
         * Kode lama tapi masih dipake
         */

        #region Halaman Penerimaan
        private Boolean addBarang(MySqlCommand _cmd)
        {
            Boolean _success = true;
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView9.SelectedRows;
                if (dataGridView9.Rows[_tempRow[0].Index].Cells[0].Value == null || textBox1.Text == "")
                {
                    _db.CloseConnection();
                    MessageBox.Show("Barang belum terpilih atau jumlah belum terisi");
                }
                else
                {
                    if (SQLCommand(_cmd, "SELECT * FROM lokasi_barang WHERE Lokasi = '" + comboBox15.Text + "' AND Barang = '" + dataGridView9.Rows[_tempRow[0].Index].Cells[0].Value.ToString() + "'").Rows.Count >= 1)
                    {
                        _cmd.CommandText = "UPDATE lokasi_barang SET Jumlah = Jumlah + " + textBox1.Text + " WHERE Barang = '" + dataGridView9.Rows[_tempRow[0].Index].Cells[0].Value.ToString() + "' AND Lokasi = '" + comboBox15.Text + "'";
                        _cmd.Prepare();
                        _cmd.ExecuteNonQuery();
                        _cmd.CommandText = "INSERT INTO log_stok (barang, masuk, ke, dari) VALUES (@barang,@masuk,@ke,@dari)";
                        _cmd.Prepare();
                        _cmd.Parameters.AddWithValue("@barang", dataGridView9.Rows[_tempRow[0].Index].Cells[0].Value.ToString());
                        _cmd.Parameters.AddWithValue("@masuk", textBox1.Text);
                        _cmd.Parameters.AddWithValue("@ke", comboBox15.Text);
                        _cmd.Parameters.AddWithValue("@dari", comboBox18.Text);
                        _cmd.ExecuteNonQuery();
                    }
                    else
                    {
                        _cmd.CommandText = "INSERT INTO lokasi_barang (Lokasi, Barang, Jumlah) VALUES (@Lokasi,@Barang,@Jumlah)";
                        _cmd.Prepare();
                        _cmd.Parameters.AddWithValue("@Lokasi", comboBox15.Text);
                        _cmd.Parameters.AddWithValue("@Barang", dataGridView9.Rows[_tempRow[0].Index].Cells[0].Value.ToString());
                        _cmd.Parameters.AddWithValue("@Jumlah", textBox1.Text);
                        _cmd.ExecuteNonQuery();
                        _cmd.CommandText = "INSERT INTO log_stok (barang, masuk, ke) VALUES (@barang,@masuk,@ke)";
                        _cmd.Prepare();
                        _cmd.Parameters.AddWithValue("@masuk", textBox1.Text);
                        _cmd.Parameters.AddWithValue("@ke", comboBox15.Text);
                        _cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                _success = false;
            }
            return _success;
        }
        private void moveLokasiBarangBaru(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView8.SelectedRows;
                if (SQLCommand(_cmd, "SELECT * FROM lokasi_barang WHERE Lokasi = '" + comboBox9.Text + "' AND Barang = '" + dataGridView8.Rows[_tempRow[0].Index].Cells[1].Value.ToString() + "'").Rows.Count >= 1)
                {
                    _cmd.CommandText = "UPDATE lokasi_barang SET Jumlah = Jumlah + " + textBox13.Text + " WHERE Lokasi = '" + comboBox9.Text + "' AND Barang = '" + dataGridView8.Rows[_tempRow[0].Index].Cells[1].Value.ToString() + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
                else
                {
                    _cmd.CommandText = "INSERT INTO lokasi_barang (Lokasi, Barang, Jumlah) VALUES (@Lokasi,@Barang,@Jumlah)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Lokasi", comboBox9.Text);
                    _cmd.Parameters.AddWithValue("@Barang", dataGridView8.Rows[_tempRow[0].Index].Cells[1].Value.ToString());
                    _cmd.Parameters.AddWithValue("@Jumlah", textBox13.Text);
                    _cmd.ExecuteNonQuery();
                }
                _cmd.CommandText = "UPDATE lokasi_barang SET Jumlah = Jumlah - " + textBox13.Text + " WHERE Lokasi = '" + dataGridView8.Rows[_tempRow[0].Index].Cells[0].Value.ToString() + "' AND Barang = '" + dataGridView8.Rows[_tempRow[0].Index].Cells[1].Value.ToString() + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
                _cmd.CommandText = "DELETE FROM lokasi_barang WHERE Jumlah = 0";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        
        //Barang2 yg bisa di click
        private void button2_Click(object sender, EventArgs e)
        {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                if (addBarang(_command)) { MessageBox.Show("Success"); };
                _db.CloseConnection();
                doTheListBox2();
                showLokasi(_command);
                textBox1.Text = "";
        }
        private void button20_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView8.SelectedRows;
            if (dataGridView8.Rows[_tempRow[0].Index].Cells[1].Value.ToString() != "")
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                moveLokasiBarangBaru(_command);
                _db.CloseConnection();
                LoadList();
                MessageBox.Show("Success");
            }
        }
        #endregion

        #region Halaman Lokasi
        string _tempLokasi;
        private void updateLokasi(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView5.SelectedRows;
                _cmd.CommandText = "UPDATE daftar_lokasi SET Lokasi = '" + dataGridView5.Rows[_tempRow[0].Index].Cells[0].Value + "' WHERE Lokasi = '" + _tempLokasi + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool insertLokasiBaru(MySqlCommand _cmd)
        {
            if (SQLCommand(_cmd, "SELECT * FROM daftar_lokasi WHERE Lokasi = '" + textBox2.Text + "'").Rows.Count >= 1)
            {
                return false;
            }
            else {
                try
                {
                    _cmd.CommandText = "INSERT INTO daftar_lokasi (Lokasi) VALUES (@Lokasi)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Lokasi", textBox2.Text);
                    _cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return true;
            }
        }
        private void setLokasi(MySqlCommand _cmd)
        {
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT * FROM daftar_lokasi ORDER BY Lokasi");
                dataGridView5.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void showLokasi(MySqlCommand _cmd)
        {
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT lokasi_barang.Lokasi,barcode_barang.Barcode,list_barang.Nama,daftar_warna.Warna,lokasi_barang.Jumlah FROM lokasi_barang LEFT OUTER JOIN barcode_barang ON barcode_barang.Barcode = lokasi_barang.Barang LEFT OUTER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode LEFT OUTER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode ORDER BY Lokasi");
                dataGridView10.DataSource = _dt;
                dataGridView8.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void showLokasi(MySqlCommand _cmd, string _barang)
        {
            try
            {
                DataTable _dt = new DataTable();
                string _where = "";
                if (_barang != null && _barang != "" && _barang != "Search...") { _where = "WHERE list_barang.Nama LIKE '" + _barang + "%'"; }
                _dt = SQLCommand(_cmd, "SELECT lokasi_barang.Lokasi,barcode_barang.Barcode,list_barang.Nama,daftar_warna.Warna,lokasi_barang.Jumlah FROM lokasi_barang LEFT OUTER JOIN barcode_barang ON barcode_barang.Barcode = lokasi_barang.Barang LEFT OUTER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode LEFT OUTER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode " + _where + " ORDER BY Lokasi");
                dataGridView8.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void moveLokasi(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView10.SelectedRows;
                _cmd.CommandText = "INSERT INTO log_stok (barang, pindah, dari, ke) VALUES (@barang, @pindah, @dari, @ke)";
                _cmd.Prepare();
                _cmd.Parameters.AddWithValue("@barang", dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value.ToString());
                _cmd.Parameters.AddWithValue("@pindah", textBox5.Text);
                _cmd.Parameters.AddWithValue("@dari", dataGridView10.Rows[_tempRow[0].Index].Cells[0].Value.ToString());
                _cmd.Parameters.AddWithValue("@ke", comboBox1.Text);
                _cmd.ExecuteNonQuery();
                if (SQLCommand(_cmd, "SELECT * FROM lokasi_barang WHERE Lokasi = '" + comboBox1.Text + "' AND Barang = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value.ToString() + "'").Rows.Count >= 1)
                {
                    _cmd.CommandText = "UPDATE lokasi_barang SET Jumlah = Jumlah + " + textBox5.Text + " WHERE Lokasi = '" + comboBox1.Text + "' AND Barang = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value.ToString() + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
                else
                {
                    _cmd.CommandText = "INSERT INTO lokasi_barang (Lokasi, Barang, Jumlah) VALUES (@Lokasi,@Barang,@Jumlah)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Lokasi", comboBox1.Text);
                    _cmd.Parameters.AddWithValue("@Jumlah", textBox5.Text);
                    _cmd.ExecuteNonQuery();
                }
                _cmd.CommandText = "UPDATE lokasi_barang SET Jumlah = Jumlah - " + textBox5.Text + " WHERE Lokasi = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[0].Value.ToString() + "' AND Barang = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value.ToString() + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
                _cmd.CommandText = "DELETE FROM lokasi_barang WHERE Jumlah = 0";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void deleteLokasi(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView5.SelectedRows;
                if (dataGridView5.RowCount < 2 || dataGridView5.Rows[_tempRow[0].Index].Cells[0].Value == null)
                {
                    MessageBox.Show("Lokasi belum terpilih");
                }
                else
                {
                    _cmd.CommandText = "DELETE FROM daftar_lokasi WHERE Lokasi = '" + dataGridView5.Rows[_tempRow[0].Index].Cells[0].Value + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void deleteLokasiBarang(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView10.SelectedRows;
                if (dataGridView10.RowCount < 2 || dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value == null)
                {
                    MessageBox.Show("Lokasi Barang belum terpilih");
                }
                else
                {
                    _cmd.CommandText = "DELETE FROM lokasi_barang WHERE Lokasi = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[0].Value + "' AND Barang = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value + "' AND Jumlah = '" + dataGridView10.Rows[_tempRow[0].Index].Cells[4].Value + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Barang2 yg bisa di click
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            if (textBox5.Text != "")
            {
                button30.Enabled = true;
            }
            else
            {
                button30.Enabled = false;
            }
        }
        private void button30_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView10.SelectedRows;
            if (dataGridView10.Rows[_tempRow[0].Index].Cells[1].Value.ToString() != "")
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                moveLokasi(_command);
                showLokasi(_command);
                _db.CloseConnection();
                MessageBox.Show("Success");
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                bool _success;
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                _success = insertLokasiBaru(_command);
                setLokasi(_command);
                _db.CloseConnection();
                if (!_success) { MessageBox.Show("Nama Tersebut Sudah Dipakai"); } else { textBox2.Text = ""; MessageBox.Show("Success"); }
            }
        }
        private void button8_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to delete?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                deleteLokasiBarang(_command);
                showLokasi(_command);
                _db.CloseConnection();
            }
        }
        private void button9_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView5.SelectedRows;
            if (MessageBox.Show("Are you sure you want to delete " + dataGridView5.Rows[_tempRow[0].Index].Cells[0].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                deleteLokasi(_command);
                setLokasi(_command);
                showLokasi(_command);
                _db.CloseConnection();
            }
        }
        private void dataGridView5_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _tempLokasi = dataGridView5.Rows[e.RowIndex].Cells[0].Value.ToString();
        }
        private void dataGridView5_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            updateLokasi(_command);
            _db.CloseConnection();
            LoadList();
        }
        #endregion

        #region Halaman Cabang
        string _tempCabang;
        private void updateCabang(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView7.SelectedRows;
                _cmd.CommandText = "UPDATE cabang SET Cabang = '" + dataGridView7.Rows[_tempRow[0].Index].Cells[0].Value + "' WHERE Cabang = '" + _tempCabang + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool insertCabangBaru(MySqlCommand _cmd)
        {
            if (SQLCommand(_cmd, "SELECT * FROM cabang WHERE Cabang = '" + textBox12.Text + "'").Rows.Count >= 1)
            {
                return false;
            }
            else
            {
                try
                {
                    _cmd.CommandText = "INSERT INTO cabang (Cabang) VALUES (@Cabang)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Cabang", textBox12.Text);
                    _cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return true;
            }
        }
        private void showCabang(MySqlCommand _cmd)
        {
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT Cabang FROM cabang ORDER BY Cabang");
                dataGridView7.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void deleteCabang(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView7.SelectedRows;
                if (dataGridView7.RowCount < 2 || dataGridView7.Rows[_tempRow[0].Index].Cells[0].Value == null)
                {
                    MessageBox.Show("Cabang belum terpilih");
                }
                else
                {
                    _cmd.CommandText = "DELETE FROM cabang WHERE Cabang = '" + dataGridView7.Rows[_tempRow[0].Index].Cells[0].Value + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Barang2 yg bisa di click
        private void button18_Click(object sender, EventArgs e)
        {
            if (textBox12.Text != "")
            {
                bool _success;
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                _success = insertCabangBaru(_command);
                _db.CloseConnection();
                LoadList();
                if (!_success) { MessageBox.Show("Cabang Tersebut Sudah Ada"); } else { textBox12.Text = ""; MessageBox.Show("Success"); }
            }
        }
        private void button17_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView7.SelectedRows;
            if (MessageBox.Show("Are you sure you want to delete " + dataGridView7.Rows[_tempRow[0].Index].Cells[0].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                deleteCabang(_command);
                _db.CloseConnection();
                LoadList();
            }
        }
        private void dataGridView7_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _tempCabang = dataGridView7.Rows[e.RowIndex].Cells[0].Value.ToString();
        }
        private void dataGridView7_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            updateCabang(_command);
            _db.CloseConnection();
            LoadList();
        }
        #endregion

        #region Halaman Barang Baru
        string _tempKodeBarang;
        string _tempNama;
        string _tempTipe;
        private void updateBarang(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView3.SelectedRows;
                _cmd.CommandText = "UPDATE list_barang SET Kode = '" + dataGridView3.Rows[_tempRow[0].Index].Cells[0].Value + "', Nama = '" + dataGridView3.Rows[_tempRow[0].Index].Cells[1].Value + "', Tipe = '" + dataGridView3.Rows[_tempRow[0].Index].Cells[2].Value + "' WHERE Kode = '" + _tempKodeBarang + "' AND Nama = '" + _tempNama + "' AND Tipe = '" + _tempTipe + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool insertBarangBaru(MySqlCommand _cmd)
        {
            if (SQLCommand(_cmd, "SELECT * FROM list_barang WHERE Kode = '" + textBox4.Text + "' OR Nama = '" + textBox6.Text + "'").Rows.Count >= 1)
            {
                return false;
            }
            else
            {
                try
                {
                    _cmd.CommandText = "INSERT INTO list_barang (Kode,Nama,Tipe) VALUES (@Kode,@Nama,@Tipe)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Kode", textBox4.Text);
                    _cmd.Parameters.AddWithValue("@Nama", textBox6.Text);
                    _cmd.Parameters.AddWithValue("@Tipe", comboBox2.Text);
                    _cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return true;
            }
        }
        private void setTipeBarang(MySqlCommand _cmd)
        {
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT DISTINCT Tipe FROM list_barang");
                comboBox2.DisplayMember = "Tipe";
                comboBox2.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void showNamaBarang(MySqlCommand _cmd)
        {
            String _where = "";
            if (textBox30.Text != "" && textBox30.Text != null && textBox30.Text != "Search...")
            {
                _where = "WHERE Nama LIKE '" + textBox30.Text + "%'";
            }
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT * FROM list_barang " + _where + " ORDER BY Tipe");
                dataGridView3.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void deleteBarang(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView3.SelectedRows;
                if (dataGridView3.RowCount < 2 || dataGridView3.Rows[_tempRow[0].Index].Cells[0].Value == null)
                {
                    MessageBox.Show("Barang belum terpilih");
                }
                else
                {
                    _cmd.CommandText = "DELETE FROM list_barang WHERE Kode = '" + dataGridView3.Rows[_tempRow[0].Index].Cells[0].Value + "' AND Nama = '" + dataGridView3.Rows[_tempRow[0].Index].Cells[1].Value + "' AND Tipe = '" + dataGridView3.Rows[_tempRow[0].Index].Cells[2].Value + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Barang2 yg bisa di click
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox4.Text != "" && textBox6.Text != "" && comboBox2.Text != "")
            {
                bool _success;
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                _success = insertBarangBaru(_command);
                _db.CloseConnection();
                LoadList();
                if (!_success) { MessageBox.Show("Nama Tersebut Sudah Dipakai"); } else { textBox4.Text = ""; textBox6.Text = ""; MessageBox.Show("Success"); }
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView3.SelectedRows;
            if (MessageBox.Show("Are you sure you want to delete " + dataGridView3.Rows[_tempRow[0].Index].Cells["Nama"].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                deleteBarang(_command);
                _db.CloseConnection();
                LoadList();
            }
        }
        private void dataGridView3_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _tempKodeBarang = dataGridView3.Rows[e.RowIndex].Cells[0].Value.ToString();
            _tempNama = dataGridView3.Rows[e.RowIndex].Cells[1].Value.ToString();
            _tempTipe = dataGridView3.Rows[e.RowIndex].Cells[2].Value.ToString();
        }
        private void dataGridView3_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            updateBarang(_command);
            _db.CloseConnection();
            LoadList();
        }
        #endregion

        #region Halaman Warna Baru
        string _tempKodeWarna;
        string _tempWarna;
        string _tempSeal;
        string _tempBase;
        string _tempInfo;
        private void updateWarna(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView4.SelectedRows;
                _cmd.CommandText = "UPDATE daftar_warna SET Kode = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[0].Value + "', Warna = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[1].Value + "', Seal = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[2].Value + "', Base = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[3].Value + "', Info = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[4].Value + "' WHERE Kode = '" + _tempKodeWarna + "' AND Warna = '" + _tempWarna + "' AND Seal = '" + _tempSeal + "' AND Base = '" + _tempBase + "' AND Info = '" + _tempInfo + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool insertWarnaBaru(MySqlCommand _cmd)
        {
            if (SQLCommand(_cmd, "SELECT * FROM daftar_warna WHERE Kode = '" + textBox10.Text + "' OR Warna = '" + textBox7.Text + "'").Rows.Count >= 1)
            {
                return false;
            }
            else
            {
                try
                {
                    _cmd.CommandText = "INSERT INTO daftar_warna (Kode,Warna,Seal,Base,Info) VALUES (@Kode,@Warna,@Seal,@Base,@Info)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Kode", textBox10.Text);
                    _cmd.Parameters.AddWithValue("@Warna", textBox7.Text);
                    _cmd.Parameters.AddWithValue("@Seal", comboBox3.Text);
                    _cmd.Parameters.AddWithValue("@Base", comboBox4.Text);
                    _cmd.Parameters.AddWithValue("@Info", textBox11.Text);
                    _cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return true;
            }
        }
        private void setWarnaSealBase(MySqlCommand _cmd)
        {
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT DISTINCT Kode FROM (SELECT DISTINCT Kode FROM daftar_warna WHERE Seal = '' AND Base = '' UNION SELECT DISTINCT Seal AS Kode FROM daftar_warna WHERE Seal IS NOT NULL) AS daftar_warna GROUP BY Kode");
                comboBox3.DisplayMember = "Kode";
                comboBox3.DataSource = _dt;
                _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT DISTINCT Kode FROM (SELECT DISTINCT Kode FROM daftar_warna WHERE Seal = '' AND Base = '' UNION SELECT DISTINCT Seal AS Kode FROM daftar_warna WHERE Seal IS NOT NULL) AS daftar_warna GROUP BY Kode");
                comboBox4.DisplayMember = "Kode";
                comboBox4.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void showDaftarWarna(MySqlCommand _cmd)
        {
            String _where = "";
            if (textBox31.Text != "" && textBox31.Text != null && textBox31.Text != "Search...")
            {
                _where = "WHERE Warna LIKE '" + textBox31.Text + "%'";
            }
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT * FROM daftar_warna " + _where + " ORDER BY Warna");
                dataGridView4.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void deleteWarna(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView4.SelectedRows;
                if (dataGridView4.RowCount < 2 || dataGridView4.Rows[_tempRow[0].Index].Cells[0].Value == null)
                {
                    MessageBox.Show("Warna belum terpilih");
                }
                else
                {
                    _cmd.CommandText = "DELETE FROM daftar_warna WHERE Kode = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[0].Value + "' AND Warna = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[1].Value + "' AND Seal = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[2].Value + "' AND Base = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[3].Value + "' AND Info = '" + dataGridView4.Rows[_tempRow[0].Index].Cells[4].Value + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Barang2 yg bisa di click
        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox10.Text != "" && textBox7.Text != "")
            {
                bool _success;
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                _success = insertWarnaBaru(_command);
                _db.CloseConnection();
                LoadList();
                if (!_success) { MessageBox.Show("Nama Tersebut Sudah Dipakai"); } else { textBox10.Text = ""; textBox7.Text = ""; textBox11.Text = ""; MessageBox.Show("Success"); }
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView4.SelectedRows;
            if (MessageBox.Show("Are you sure you want to delete " + dataGridView4.Rows[_tempRow[0].Index].Cells["Warna"].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                deleteWarna(_command);
                _db.CloseConnection();
                LoadList();
            }
        }
        private void dataGridView4_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _tempKodeWarna = dataGridView4.Rows[e.RowIndex].Cells[0].Value.ToString();
            _tempWarna = dataGridView4.Rows[e.RowIndex].Cells[1].Value.ToString();
            _tempSeal = dataGridView4.Rows[e.RowIndex].Cells[2].Value.ToString();
            _tempBase = dataGridView4.Rows[e.RowIndex].Cells[3].Value.ToString();
            _tempInfo = dataGridView4.Rows[e.RowIndex].Cells[4].Value.ToString();
        }
        private void dataGridView4_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            updateWarna(_command);
            _db.CloseConnection();
            LoadList();
        }
        #endregion

        #region Halaman Penggabungan
        string _tempBarcode;
        string _tempBarcodeBarang;
        string _tempBarcodeWarna;
        private void updateBarcode(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView6.SelectedRows;
                _cmd.CommandText = "UPDATE barcode_barang SET Barcode = '" + dataGridView6.Rows[_tempRow[0].Index].Cells[0].Value + "', Barang = '" + dataGridView6.Rows[_tempRow[0].Index].Cells[1].Value + "', Warna = '" + dataGridView6.Rows[_tempRow[0].Index].Cells[2].Value + "' WHERE Barcode = '" + _tempBarcode + "' AND Barang = '" + _tempBarcodeBarang + "' AND Warna = '" + _tempBarcodeWarna + "'";
                _cmd.Prepare();
                _cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool insertBarcodeBaru(MySqlCommand _cmd)
        {
            if (SQLCommand(_cmd, "SELECT * FROM barcode_barang WHERE Barcode = '" + textBox14.Text + "'").Rows.Count >= 1)
            {
                return false;
            }
            else
            {
                try
                {
                    string _tempBarang = "";
                    string _tempWarna = "";
                    DataTable _dt = new DataTable();
                    _dt = SQLCommand(_cmd, "SELECT DISTINCT Kode FROM list_barang WHERE Nama = '" + comboBox6.Text + "'");
                    DataRow _tempRow = null;
                    foreach (DataRow _tempRow_Variable in _dt.Rows)
                    {
                        _tempRow = _tempRow_Variable;
                        _tempBarang = _tempRow["Kode"].ToString();
                    }
                    _dt = new DataTable();
                    _dt = SQLCommand(_cmd, "SELECT DISTINCT Kode FROM daftar_warna WHERE Warna = '" + comboBox5.Text + "'");
                    _tempRow = null;
                    foreach (DataRow _tempRow_Variable in _dt.Rows)
                    {
                        _tempRow = _tempRow_Variable;
                        _tempWarna = _tempRow["Kode"].ToString();
                    }
                    _cmd.CommandText = "INSERT INTO barcode_barang (Barcode,Barang,Warna) VALUES (@Barcode,@Barang,@Warna)";
                    _cmd.Prepare();
                    _cmd.Parameters.AddWithValue("@Barcode", textBox14.Text);
                    _cmd.Parameters.AddWithValue("@Barang", _tempBarang);
                    _cmd.Parameters.AddWithValue("@Warna", _tempWarna);
                    _cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                return true;
            }
        }
        private void setBarcode(MySqlCommand _cmd)
        {
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT Nama FROM list_barang GROUP BY Nama ORDER BY Nama");
                comboBox6.DisplayMember = "Nama";
                comboBox6.DataSource = _dt;
                _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT Warna FROM daftar_warna ORDER BY Warna");
                comboBox5.DisplayMember = "Warna";
                comboBox5.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void showBarcode(MySqlCommand _cmd)
        {
            String _where = "";
            if (textBox32.Text != "" && textBox32.Text != null && textBox32.Text != "Search...")
            {
                _where = "WHERE list_barang.Nama LIKE '" + textBox32.Text + "%'";
            }
            try
            {
                DataTable _dt = new DataTable();
                _dt = SQLCommand(_cmd, "SELECT barcode_barang.*, list_barang.Nama, daftar_warna.Warna FROM barcode_barang INNER JOIN list_barang ON barcode_barang.Barang = list_barang.Kode INNER JOIN daftar_warna ON barcode_barang.Warna = daftar_warna.Kode " + _where + " ORDER BY Barcode");
                dataGridView6.DataSource = _dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void deleteBarcode(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView6.SelectedRows;
                if (dataGridView6.RowCount < 2 || dataGridView6.Rows[_tempRow[0].Index].Cells[0].Value == null)
                {
                    MessageBox.Show("Barcode belum terpilih");
                }
                else
                {
                    _cmd.CommandText = "DELETE FROM barcode_barang WHERE Barcode = '" + dataGridView6.Rows[_tempRow[0].Index].Cells[0].Value + "' AND Barang = '" + dataGridView6.Rows[_tempRow[0].Index].Cells[1].Value + "' AND Warna = '" + dataGridView6.Rows[_tempRow[0].Index].Cells[2].Value + "'";
                    _cmd.Prepare();
                    _cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Barang2 yg bisa di click
        private void button13_Click(object sender, EventArgs e)
        {
            if (textBox14.Text != "")
            {
                bool _success;
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                _success = insertBarcodeBaru(_command);
                showBarcode(_command);
                _db.CloseConnection();
                if (!_success) { MessageBox.Show("Barcode Tersebut Sudah Dipakai"); } else { textBox14.Text = ""; MessageBox.Show("Success"); }
            }
        }
        private void button14_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection _tempRow = dataGridView6.SelectedRows;
            if (MessageBox.Show("Are you sure you want to delete " + dataGridView6.Rows[_tempRow[0].Index].Cells["Nama"].Value.ToString() + " " + dataGridView6.Rows[_tempRow[0].Index].Cells["Warna1"].Value.ToString() + "?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                MySqlCommand _command = _db.OpenConnection().CreateCommand();
                deleteBarcode(_command);
                _db.CloseConnection();
                LoadList();
            }
        }
        private void dataGridView6_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            _tempBarcode = dataGridView6.Rows[e.RowIndex].Cells[0].Value.ToString();
            _tempBarcodeBarang = dataGridView6.Rows[e.RowIndex].Cells[1].Value.ToString();
            _tempBarcodeWarna = dataGridView6.Rows[e.RowIndex].Cells[2].Value.ToString();
        }
        private void dataGridView6_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            MySqlCommand _command = _db.OpenConnection().CreateCommand();
            updateBarcode(_command);
            _db.CloseConnection();
            LoadList();
        }
        #endregion

        #region Halaman PO
        private void insertPO(MySqlCommand _cmd)
        {
            try
            {
                DataGridViewSelectedRowCollection _tempRow = dataGridView12.SelectedRows;
                if (dataGridView12.Rows[_tempRow[0].Index].Cells[0].Value == null || textBox9.Text == "" || comboBox11.Text == "")
                {
                    _db.CloseConnection();
                    MessageBox.Show("Barang belum terpilih atau jumlah belum terisi");
                }
                else
                {
                    DataRow tempRow = null;
                    String _cabang = "";
                    String _barang = "";
                    foreach (DataRow tempRow_Variable in SQLCommand(_cmd, "SELECT Cabang FROM po WHERE PO = '" + comboBox11.Text + "'").Rows)
                    {
                        tempRow = tempRow_Variable;
                        _cabang += tempRow["Cabang"].ToString();
                    }
                    foreach (DataRow tempRow_Variable in SQLCommand(_cmd, "SELECT Barang FROM po_detail WHERE PO = '" + comboBox11.Text + "'").Rows)
                    {
                        tempRow = tempRow_Variable;
                        _barang += tempRow["Barang"].ToString();
                    }
                    //Check apakah nomor PO sudah ada di database atau belum
                    if (SQLCommand(_cmd, "SELECT PO FROM po WHERE PO = '" + comboBox11.Text + "'").Rows.Count >= 1)
                    {
                        //Check apakah cabang yg dipilih sesuai dengan cabang nomor PO yg sudah ada di database
                        if (_cabang == comboBox7.Text)
                        {
                            //Check apakah barang sudah ada di dalam nomor PO tersebut atau belum
                            if (_barang == dataGridView12.Rows[_tempRow[0].Index].Cells["Barcode"].Value.ToString())
                            {
                                _cmd.CommandText = "UPDATE po_detail SET Jumlah = Jumlah + " + textBox9.Text + " WHERE PO = '" + comboBox11.Text + "' AND Barang = '" + dataGridView12.Rows[_tempRow[0].Index].Cells["Barcode"].Value.ToString() + "'";
                                _cmd.Prepare();
                                _cmd.ExecuteNonQuery();
                            }
                            //Bikin detail PO baru bila belum ada barang tersebut
                            else
                            {
                                _cmd.CommandText = "INSERT INTO po_detail (PO, Barang, Jumlah, Kirim) VALUES (@PO,@Barang,@Jumlah,@Kirim)";
                                _cmd.Prepare();
                                _cmd.Parameters.AddWithValue("@PO", comboBox11.Text);
                                _cmd.Parameters.AddWithValue("@Barang", dataGridView12.Rows[_tempRow[0].Index].Cells["Barcode"].Value);
                                _cmd.Parameters.AddWithValue("@Jumlah", textBox9.Text);
                                _cmd.Parameters.AddWithValue("@Kirim", 0);
                                _cmd.ExecuteNonQuery();
                            }
                        }
                        //Show error
                        else
                        {
                            MessageBox.Show("Nomor PO tersebut sudah dipakai oleh cabang " + _cabang);
                        }
                    }
                    //Bikin nomor PO dan detailnya bila belum ada di database
                    else
                    {
                        _cmd.CommandText = "INSERT INTO po (PO, Cabang) VALUES (@PO,@Cabang)";
                        _cmd.Prepare();
                        _cmd.Parameters.AddWithValue("@PO", comboBox11.Text);
                        _cmd.Parameters.AddWithValue("@Cabang", comboBox7.Text);
                        _cmd.ExecuteNonQuery();
                        _cmd.CommandText = "INSERT INTO po_detail (PO, Barang, Jumlah, Kirim) VALUES (@PO,@Barang,@Jumlah,@Kirim)";
                        _cmd.Prepare();
                        _cmd.Parameters.AddWithValue("@Barang", dataGridView12.Rows[_tempRow[0].Index].Cells["Barcode"].Value);
                        _cmd.Parameters.AddWithValue("@Jumlah", textBox9.Text);
                        _cmd.Parameters.AddWithValue("@Kirim", 0);
                        _cmd.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        

        


    }
}
